package com.enba.rbacadmin.module.auth.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enba.boot.core.base.Result;
import com.enba.rbacadmin.annotation.DistributedLock;
import com.enba.rbacadmin.annotation.NoNeedValidation;
import com.enba.rbacadmin.module.auth.controller.req.LoginReq;
import com.enba.rbacadmin.module.auth.controller.req.RegisterReq;
import com.enba.rbacadmin.module.auth.controller.req.UpdatePasswordReq;
import com.enba.rbacadmin.module.auth.controller.req.UserAddUpdateReq;
import com.enba.rbacadmin.module.auth.controller.req.UserBindRoleReq;
import com.enba.rbacadmin.module.auth.controller.req.UserPageReq;
import com.enba.rbacadmin.module.auth.controller.resp.UserPageResp;
import com.enba.rbacadmin.module.auth.entity.User;
import com.enba.rbacadmin.module.auth.service.IUserService;
import com.enba.rbacadmin.utils.CodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户表
 *
 * @author 恩爸编程
 * @since 2025-02-28
 */
@RestController
@RequestMapping("/auth/user")
@Api(tags = "用户表接口")
@Slf4j
public class UserController {

  @Resource private IUserService userService;
  @Resource private CodeGenerator codeGenerator;

  /**
   * 根据id查询数据接口
   *
   * @param id id
   * @return result
   */
  @ApiOperation(value = "用户详情")
  @GetMapping("/{id}")
  public Result<User> findOne(@PathVariable Integer id) {
    User byId = userService.getById(id);
    return Result.success(byId);
  }

  /**
   * 分页查询接口
   *
   * @param req r
   * @return result
   */
  @PostMapping("/page")
  public Result<List<UserPageResp>> userFindPage(@RequestBody @Validated UserPageReq req) {
    Page<UserPageResp> userPage = userService.userFindPage(req);

    return Result.pageSuccess(
        BeanUtil.copyToList(userPage.getRecords(), UserPageResp.class), userPage.getTotal());
  }

  /**
   * 新增和更新接口
   *
   * @param req user
   * @return result
   */
  @DistributedLock
  @PostMapping
  public Result<Boolean> saveOrUpdate(@RequestBody @Validated UserAddUpdateReq req) {
    return Result.success(userService.saveOrUpdate(req));
  }

  /**
   * 删除接口
   *
   * @param id id
   * @return result
   */
  @DeleteMapping("/{id}")
  public Result<Boolean> delete(@PathVariable Integer id) {
    return Result.success(userService.delete(id));
  }

  /**
   * 批量删除接口
   *
   * @param ids ids
   * @return result
   */
  @PostMapping("/del/batch")
  public Result<Boolean> deleteBatch(
      @RequestBody @Validated @NotNull(message = "id不能为空") List<Integer> ids) {
    return Result.success(userService.deleteBatch(ids));
  }

  @ApiOperation(value = "注册")
  @PostMapping("/register")
  public Result<Boolean> register(@RequestBody @Validated RegisterReq req) {
    return Result.success(userService.register(req));
  }

  @ApiOperation(value = "登陆")
  @PostMapping("/login")
  @DistributedLock
  public Result<String> login(@RequestBody @Validated LoginReq req) {
    return Result.success(userService.login(req));
  }

  @ApiOperation(value = "退出登录")
  @GetMapping("/logout")
  @DistributedLock
  public Result<Boolean> logout() {
    return Result.success(userService.logout());
  }

  // 获取组织机构编码
  @NoNeedValidation
  @ApiOperation("获取用户编码测试")
  @GetMapping("/userGenerateCode")
  public Result<String> getUserCode() {
    return Result.success(codeGenerator.userGenerateCode());
  }

  // 用户绑定角色
  @ApiOperation("用户绑定角色")
  @PostMapping("/bindRole")
  @DistributedLock
  public Result<Boolean> bindRole(@RequestBody @Validated UserBindRoleReq req) {
    return Result.success(userService.bindRole(req));
  }

  // 修改密码
  @ApiOperation("修改密码")
  @PostMapping("/updatePassword")
  @DistributedLock
  public Result<Boolean> updatePassword(@RequestBody @Validated UpdatePasswordReq req) {
    return Result.success(userService.updatePassword(req));
  }
}
